package a02_链表;

/**
 * <p>
 * a05_两两交换链表中的节点复习5
 * </p>
 *
 * @author flyduck
 * @since 2025/1/2
 */
public class a05_两两交换链表中的节点复习5 {
    //dummyNode 1 2 3 4
    //dummyNode 2 1 3 4
    public ListNode swapPairs(ListNode head) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;

        ListNode currentNode = dummyNode;
        while (currentNode != null && currentNode.next != null && currentNode.next.next != null) {
            ListNode firstNode = currentNode.next;
            ListNode secondNode = firstNode.next;
            ListNode thirdNode = secondNode.next;

            currentNode.next = secondNode;
            secondNode.next = firstNode;
            firstNode.next = thirdNode;

            currentNode = firstNode;
        }
        return dummyNode.next;
    }
}
